﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;

namespace SS
{
    /// <summary>
    /// 获取代理 IP,端口
    /// http://www.net.cn/static/customercare/yourIP.asp
    /// </summary>
    public class GetProxy
    {
        private  List<WebProxy> ADRESS_PROXY_LIST;

        public GetProxy()
        {
            ADRESS_PROXY_LIST = new List<WebProxy>();
             
             
        }
        
        /// <summary>
        /// 获取1
        /// </summary>
        /// <returns></returns>
        public static List<WebProxy> GetProxyProxycn()
        {
            List<WebProxy> adressProxyList = new List<WebProxy>();
            AdressInfo adressInfo= new AdressInfo() { 
                Adress = "http://www.proxycn.com/html_proxy/30fastproxy-1.html", 
                Directions = "今日最新最快50个HTTP代理列表" };
            WebClient web = new WebClient();
            string result = "";
            try
            {
                result = web.DownloadString(adressInfo.Adress);
            }
            catch (Exception)
            {
                return adressProxyList;
            }
           
            //"clip('204.85.191.10:68664');
            string startStr="\"clip\\('\\d{1,5}\\.\\d{1,5}\\.\\d{1,5}\\.\\d{1,5}:\\d{0,7}";
            MatchData md = new MatchData(startStr, "'\\)", result);
            Match[] matches= md.Matches(System.Text.RegularExpressions.RegexOptions.None);
            for (int i = 0; i < matches.Length; i++)
            {
                try
                {
                    string hostPort = matches[i].Value.TrimStart("\"clip\\('".ToCharArray()).TrimEnd("'\\)".ToCharArray());
                    string[] aa = hostPort.Split(':');
                    if (aa.Length==1)
                    {
                        WebProxy webProxy = new WebProxy(aa[0], 80);
                        adressProxyList.Add(webProxy);
                    }
                    else if (aa.Length>=2)
                    {
                        WebProxy webProxy = new WebProxy(aa[0],Convert.ToInt32(aa[1]));
                        adressProxyList.Add(webProxy);
                    }
                }
                catch (Exception e)
                {
                    VoteLog.AddResult("app.fei", "value="+matches[i].Value+"&error:"+e.Message);
                }
            }
            return adressProxyList;
        }

        public static List<IPEndPoint> GetProxyProxycn2()
        {
            List<IPEndPoint> adressProxyList = new List<IPEndPoint>();
            AdressInfo adressInfo = new AdressInfo()
            {
                Adress = "http://www.proxycn.com/html_proxy/30fastproxy-1.html",
                Directions = "今日最新最快50个HTTP代理列表"
            };
            WebClient web = new WebClient();
            string result = "";
            try
            {
                result = web.DownloadString(adressInfo.Adress);
            }
            catch (Exception)
            {
                return adressProxyList;
            }

            //"clip('204.85.191.10:68664');
            string startStr = "\"clip\\('\\d{1,5}\\.\\d{1,5}\\.\\d{1,5}\\.\\d{1,5}:\\d{0,7}";
            MatchData md = new MatchData(startStr, "'\\)", result);
            Match[] matches = md.Matches(System.Text.RegularExpressions.RegexOptions.None);
            for (int i = 0; i < matches.Length; i++)
            {
                try
                {
                    string hostPort = matches[i].Value.TrimStart("\"clip\\('".ToCharArray()).TrimEnd("'\\)".ToCharArray());
                    string[] aa = hostPort.Split(':');
                    if (aa.Length == 1)
                    {
                        IPEndPoint ipeProxy = new IPEndPoint(IPAddress.Parse(aa[0]), 80);
                        adressProxyList.Add(ipeProxy);
                    }
                    else if (aa.Length >= 2)
                    {
                        IPEndPoint ipeProxy = new IPEndPoint(IPAddress.Parse(aa[0]), Convert.ToInt32(aa[1]));
                        adressProxyList.Add(ipeProxy);
                    }
                }
                catch (Exception e)
                {
                    VoteLog.AddResult("app.fei", "value=" + matches[i].Value + "&error:" + e.Message);
                }
            }
            return adressProxyList;
        }
    }
}
